package  
{
  import com.bit101.components.Label;
  import com.bit101.components.NumericStepper;
  import com.bit101.components.PushButton;
  import com.bit101.components.Window;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  
	/**
   * ...
   * @author Ivan
   */
  public class DebugPanel extends Sprite
  {
    
    public function DebugPanel(board:GameBoard) 
    {
      this.board = board;
      
      window = new Window(this, 0, 0, "The RTS Debug Options");
      window.hasMinimizeButton = true;
      window.width = 400;
      window.height = 500;
      
      new Label(window, 10, 10, "Towns");
      towns = new NumericStepper(window, 50, 10);
      towns.minimum = 2;
      towns.maximum = 100;
      towns.value = 10;
      
      new Label(window, 10, 30, "Players");
      var players:NumericStepper = new NumericStepper(window, 50, 30, onPlayersChange);
      players.minimum = 2;
      players.maximum = 8;
      
      var colors:Array = [0x4747FF, 0x47FF47, 0x47FFFF, 0xFF4747, 0xFF47FF, 0xFFFF47, 0x773377, 0x337733];
      for (var i:int = 0; i < 8; i++)
        playerPanels.push(new PlayerPanel(window, 10, 50 + i * 45, i+1));
      for (i = 2; i < 8; i++)
        playerPanels[i].enabled = false;
      for (i = 0; i < 8; i++)
        playerPanels[i].defaultColor = colors[i];
        
      var start:PushButton = new PushButton(window, 150, 450, "Start Game", startGame);
    }
    
    private function onPlayersChange(e:Event):void
    {
      var players:int = (e.target as NumericStepper).value;
      for (var i:int = 0; i < 8; i++)
        playerPanels[i].enabled = i < players;
    }
    
    private function startGame(e:Event = null):void
    {
      board.restart(0);
      board.createTowns(towns.value);
      var townOrder:Vector.<int> = new Vector.<int>();
      for (i = 0; i < towns.value; i++)
        townOrder.push(i);
      townOrder = randomizeVector(townOrder);
      for (var i:int = 0; i < playerPanels.length; i++)
      {
        var panel:PlayerPanel = playerPanels[i];
        if (!panel.enabled) break;
        
        var player:Player = new Player();
        player.name = panel.playerName;
        player.color = panel.defaultColor;
        if (!panel.human) player.controller = new AIController(panel.skill);
        board.createPlayer(player, GameBoard.towns[townOrder[i]]);
      }
      
    }
    
    private function randomizeVector(v:Vector.<int>):Vector.<int>
    {
      var ans:Vector.<int> = new Vector.<int>();
      while (v.length > 0)
        ans.push(v.splice(Math.round(Math.random() * (v.length - 1)), 1)[0]);
      return ans;
    }
    
    private var window:Window;
    private var towns:NumericStepper;
    private var board:GameBoard;
    private var playerPanels:Vector.<PlayerPanel> = new Vector.<PlayerPanel>();
    
  }

}

import com.bit101.components.ColorChooser;
import com.bit101.components.ComboBox;
import com.bit101.components.InputText;
import com.bit101.components.Panel;
import com.bit101.components.RadioButton;
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import flash.utils.Dictionary;

internal class PlayerPanel extends Panel
{
  public var human:Boolean;
  public var playerName:String;
  
  public function PlayerPanel(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, n:int = 0)
  {
    super(parent, xpos, ypos);
    width = 380;
    height = 40;
    
    playerName = "Player " + n.toString();
    var name:InputText = new InputText(this, 10, 12, playerName, onPlayerNameChange);
    colorChooser = new ColorChooser(this, 120, 12);
    colorChooser.usePopup = true;
    
    var r1:RadioButton = new RadioButton(this, 200, 10, "human", n == 1, onHuman);
    var r2:RadioButton = new RadioButton(this, 200, 25, "CPU", n != 1, onCPU);
    r1.groupName = r2.groupName = n.toString();
    human = r1.selected;
    
    var skills:Array = ["Easy", "Hard"];
    skillComboBox = new ComboBox(this, 260, 12, "", skills);
    skillComboBox.selectedIndex = 0;
    skillComboBox.numVisibleItems = skills.length;
  }
  
  public function get skill():String
  {
    var d:Dictionary = new Dictionary();
    d[0] = AIController.MODE_EASY;
    d[1] = AIController.MODE_HARD;
    return d[skillComboBox.selectedIndex];
  }
  
  public function set defaultColor(val:uint):void
  {
    colorChooser.value = val;
  }
  
  public function get defaultColor():uint { return colorChooser.value; }
  
  private function onPlayerNameChange(e:Event):void
  {
    playerName = (e.target as InputText).text;
    trace(playerName);
  }
  
  private function onHuman(e:Event):void
  {
    human = true;
    skillComboBox.enabled = false;
  }
  
  private function onCPU(e:Event):void
  {
    human = false;
    skillComboBox.enabled = true;
  }
  
  private var colorChooser:ColorChooser;
  private var skillComboBox:ComboBox;
}